package com.fitapp;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;

public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener
{
	private static final String TAG = "CameraTest";
	Camera mCamera;
	boolean mPreviewRunning = false;

	@SuppressWarnings( "deprecation" )
	public void onCreate( Bundle icicle )
	{
		super.onCreate( icicle );
		getWindow( ).setFormat( PixelFormat.TRANSLUCENT );
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		getWindow( ).setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView( R.layout.cameraview );
		mSurfaceView = (SurfaceView) findViewById( R.id.surface_camera );
		mSurfaceView.setOnClickListener( this );
		mSurfaceHolder = mSurfaceView.getHolder( );
		mSurfaceHolder.addCallback( this );
		mSurfaceHolder.setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
	}

	@Override
	protected void onRestoreInstanceState( Bundle savedInstanceState )
	{
		super.onRestoreInstanceState( savedInstanceState );
	}

	Camera.PictureCallback mPictureCallback = new Camera.PictureCallback( )
	{

		@Override
		public void onPictureTaken( byte[ ] data, Camera camera )
		{
			// TODO Auto-generated method stub
			if( data != null )
			{
				mCamera.stopPreview( );
				mPreviewRunning = false;
				mCamera.release( );
				try
				{
					BitmapFactory.Options opts = new BitmapFactory.Options( );
					Bitmap bitmap = BitmapFactory.decodeByteArray( data, 0, data.length, opts );
					bitmap = Bitmap.createScaledBitmap( bitmap, 480, 480, false );
					LightSensorSportArtsActivity.image.setImageBitmap( bitmap );
					LightSensorSportArtsActivity.image.setVisibility( View.VISIBLE );
				}
				catch( Exception e )
				{
					e.printStackTrace( );
				}
				setResult( 585 );
				finish( );
			}
		}
	};

	protected void onResume()
	{
		Log.e( TAG, "onResume" );
		super.onResume( );
	}

	protected void onSaveInstanceState( Bundle outState )
	{
		super.onSaveInstanceState( outState );
	}

	protected void onStop()
	{
		Log.e( TAG, "onStop" );
		super.onStop( );
	}

	public void surfaceCreated( SurfaceHolder holder )
	{
		mCamera = Camera.open( CameraInfo.CAMERA_FACING_FRONT );
	}

	public void surfaceChanged( SurfaceHolder holder, int format, int w, int h )
	{

		// XXX stopPreview() will crash if preview is not running
		if( mPreviewRunning )
		{
			mCamera.stopPreview( );
		}

		Camera.Parameters parameters = mCamera.getParameters( );
		List< Camera.Size > previewSizes = parameters.getSupportedPreviewSizes( );

		Camera.Size previewSize = previewSizes.get( 1 );
		parameters.setPreviewSize( previewSize.width, previewSize.height );
		parameters.setRotation( 270 );
		mCamera.setParameters( parameters );
		mCamera.startPreview( );

		mCamera.setParameters( parameters );
		try
		{
			mCamera.setPreviewDisplay( holder );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}

		mCamera.takePicture( null, mPictureCallback, mPictureCallback );
	}

	@Override
	public void surfaceDestroyed( SurfaceHolder holder )
	{
		// Log.e(TAG, "surfaceDestroyed");
		mCamera.release( );
	}

	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;

	@Override
	public void onClick( View v )
	{
		mCamera.takePicture( null, mPictureCallback, mPictureCallback );
	}
}